package com.zhongli.devplatform.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.dto.sys.MenuDTO;
import com.zhongli.devplatform.mapper.sys.*;
import com.zhongli.devplatform.po.user.ReadMenuIconPO;
import com.zhongli.devplatform.po.user.ReadMenuPO;
import com.zhongli.devplatform.po.user.ReadRolePO;
import com.zhongli.devplatform.service.imp.common.ReadMenuService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.MenuVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author  WuXiCheng
 * @date  2020/7/23 10:12
 * @version 1.0
 */
@Service
public class ReadMenuServiceImpl extends ServiceImpl<ReadMenuMapper, ReadMenuPO> implements ReadMenuService {
    @Autowired
    private ReadMenuMapper menuMapper;

    @Autowired
    private ReadRoleMapper roleMapper;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private ReadMenuComponentMapper readMenuComponentMapper;

    @Autowired
    private ReadMenuIconMapper menuIconMapper;

    @Autowired
    private ReadRoleMenuMapper roleMenuMapper;

    @Value("${dev-platform.cache-name}")
    private String cacheName;


    @Override
    public List<MenuVO> list(Integer roleId) {
        QueryWrapper queryWrapper = new QueryWrapper<ReadMenuPO>().isNull("parent_id").orderByAsc("order_num");
        if (roleId != null) {
            queryWrapper.inSql("id", "select menu_id from read_role_menu where role_id = " + roleId);
        }
        List<ReadMenuPO> menuPOList = menuMapper.selectList(queryWrapper);
        List<MenuVO> menuVOList = BeanCopierUtil.copy(menuPOList, MenuVO.class);
        for (MenuVO menuVO : menuVOList) {
            if (menuVO.getMenuComponentId() != null) {
                menuVO.setComponent(readMenuComponentMapper.selectById(menuVO.getMenuComponentId()).getCode());
            }
            if (menuVO.getMenuIconId() != null) {
                ReadMenuIconPO menuIconPO = menuIconMapper.selectById(menuVO.getMenuIconId());
                menuVO.setIcon(menuIconPO.getCode());
                menuVO.setMenuIconType(menuIconPO.getType());
            }

            if (menuVO.getMenuSelectIconId() != null) {

                menuVO.setSelectIcon(menuIconMapper.selectById(menuVO.getMenuSelectIconId()).getCode());

            }

            listChildren(menuVO, roleId);
        }

        return menuVOList;
    }

    private void listChildren(MenuVO parent, Integer roleId) {
        QueryWrapper queryWrapper = new QueryWrapper<ReadMenuPO>().eq("parent_id", parent.getId()).orderByAsc("order_num");
        if (roleId != null) {
            queryWrapper.inSql("id", "select menu_id from read_role_menu where role_id = " + roleId);
        }
        List<ReadMenuPO> menuPOList = menuMapper.selectList(queryWrapper);
        if (menuPOList.isEmpty()) {
            return;
        }
        List<MenuVO> menuVOList = BeanCopierUtil.copy(menuPOList, MenuVO.class);
        parent.setChildren(menuVOList);
        for (MenuVO menuVO : menuVOList) {
            if (menuVO.getMenuComponentId() != null) {
                menuVO.setComponent(readMenuComponentMapper.selectById(menuVO.getMenuComponentId()).getCode());
            }
            if (menuVO.getMenuIconId() != null) {
                ReadMenuIconPO menuIconPO = menuIconMapper.selectById(menuVO.getMenuIconId());
                menuVO.setIcon(menuIconPO.getCode());
                menuVO.setMenuIconType(menuIconPO.getType());
            }

            if (menuVO.getMenuSelectIconId() != null) {
                menuVO.setSelectIcon(menuIconMapper.selectById(menuVO.getMenuSelectIconId()).getCode());
            }

            menuVO.setParentMetaTitle(parent.getMetaTitle());
            listChildren(menuVO, roleId);
        }
        parent.setChildren(menuVOList);
    }


    @Override
    @PostConstruct
    public void putCache() {
        List<ReadRolePO> rolePOList = roleMapper.selectList(null);
        for (ReadRolePO rolePO : rolePOList) {
            List<MenuVO> menuVOList = list(rolePO.getId());
            cacheManager.getCache(cacheName + CACHE_KEY).put(rolePO.getId(), menuVOList);
        }
    }

    @Override
    @Transactional
    public void save(MenuDTO menuDTO) {
        ReadMenuPO menuPO = BeanCopierUtil.copy(menuDTO, ReadMenuPO.class);
        if (StringUtils.containsAny(menuDTO.getMenuIconType(), "2", "3", "4")) {
            ReadMenuIconPO menuIconPO = new ReadMenuIconPO();
            menuIconPO.setName(menuDTO.getIcon());
            menuIconPO.setCode(menuDTO.getIcon());
            menuIconPO.setType(menuDTO.getMenuIconType());
            menuIconMapper.insert(menuIconPO);
            menuPO.setMenuIconId(menuIconPO.getId());

        }

        if (!StringUtils.isBlank(menuDTO.getSelectIcon())) {

            ReadMenuIconPO menuIconPO = new ReadMenuIconPO();
            menuIconPO.setName(menuDTO.getSelectIcon());
            menuIconPO.setCode(menuDTO.getSelectIcon());
            menuIconPO.setType(menuDTO.getMenuIconType());
            menuIconMapper.insert(menuIconPO);
            menuPO.setMenuSelectIconId(menuIconPO.getId());

        }

        if (menuPO.getId() == null) {
            menuMapper.insert(menuPO);
        } else {
            menuMapper.updateById(menuPO);

            if (StringUtils.containsAny(menuDTO.getMenuIconType(), "2", "3", "4")) {
                ReadMenuIconPO menuIconPO = new ReadMenuIconPO();
                menuIconPO.setName(menuDTO.getIcon());
                menuIconPO.setCode(menuDTO.getIcon());
                menuIconPO.setType(menuDTO.getMenuIconType());
                menuIconPO.setId(menuDTO.getMenuIconId());
                menuIconMapper.updateById(menuIconPO);
            }

            if (!StringUtils.isBlank(menuDTO.getSelectIcon())) {

                ReadMenuIconPO menuIconPO = new ReadMenuIconPO();
                menuIconPO.setName(menuDTO.getSelectIcon());
                menuIconPO.setCode(menuDTO.getSelectIcon());
                menuIconPO.setType(menuDTO.getMenuIconType());
                menuIconPO.setId(menuDTO.getMenuSelectIconId());
                menuIconMapper.updateById(menuIconPO);

            }

        }

    }

    @Override
    public void delete(Integer id) {
        menuMapper.deleteById(id);
    }

    @Override
    public List<Integer> listMenuIdByRole(Integer roleId) {
        return roleMenuMapper.listMenuIdByRole(roleId);
    }
}
